"""
下面是测试代码
"""
import json
import time
import numpy as np
from B_plus import BPT,build_tree,visual
def test(keys:list,values:list,number:int=20): # 测试函数，用于测试程序是否健壮
    keys=np.array(keys)
    values=np.array(values)
    bpt=BPT(eps=4)
    for i in range(number):
        # 建立一棵B+树
        shuffle_index = np.random.permutation(len(keys))
        shuffle_keys=keys[shuffle_index]
        shuffle_values=values[shuffle_index]
        for key,value in zip(shuffle_keys,shuffle_values):
            bpt.insert(key,value)
        # 开展删除操作
        delete_index=np.random.permutation(len(keys))
        delete_keys=keys[delete_index]
        print("-------------------------------------------")
        print("建立的序列：",end=" ")
        print(shuffle_keys)
        print("删除的序列：",end=" ")
        print(delete_keys)
        print("正在删除：",end=" ")
        for key in delete_keys:
            print(key,end=" ")
            time.sleep(0.1)
            bpt.delete(key)
        print()



if __name__ == '__main__':
    arr=[55,97,15,44,72,10,21,37,51,63,85,91,100]
    values=["五十五","九十七","十五","四十四","七十二","十","二十一","三十七","五十一","六十三","八十五","九十一","一百"]
    # test(arr,values,20)
    bpt=build_tree(arr,values)
    result=visual(bpt)
    jsonstr=json.dumps(result)
    print(jsonstr)